package com.fz.mybatis;

import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.List;
import java.util.Map;


/**
 * Created by webrx on 2017-08-23.
 */
public class MybaisHelp<T> {
    private SqlSessionFactory sf;
    private SqlSession ss;
    private Class t;

    public MybaisHelp() {

        try {
            InputStream i = Resources.getResourceAsStream("book.xml");
            this.sf = new SqlSessionFactoryBuilder().build(i);
            this.ss = this.sf.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
    public MybaisHelp(Class t) {

        try {
            InputStream i = Resources.getResourceAsStream("book.xml");
            this.sf = new SqlSessionFactoryBuilder().build(i);
            this.ss = this.sf.openSession();
            this.t = t;
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    //反回范型getMapper
    public T getMapper(){
        return (T)this.ss.getMapper(this.t);
    }


    //
    public void close() {
        if (this.ss != null) {
            this.ss.commit();
            this.ss.clearCache();
            this.ss.close();
        }
    }

    public SqlSessionFactory getSf() {
        return sf;
    }

    public void setSf(SqlSessionFactory sf) {
        this.sf = sf;
    }

    public SqlSession getSs() {
        return ss;
    }

    public void setSs(SqlSession ss) {
        this.ss = ss;
    }


}
